package com.uenpay.im.panel;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qyx.android.weight.utils.QyxWeightDensityUtils;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.publicnumber.PublicNumMenu;

import java.util.ArrayList;

public class MyPopupWindowBottom extends PopupWindow {
	// 点击回调
	private IPublicNumClickListener iPopupWindowListener;
	private Context mContext;
	// 实例化一个矩�?
	private Rect mRect = new Rect();
	// 坐标的位置（x、y�?
	private final int[] mLocation = new int[2];
	// 位置不在中心
	private int popupGravity = Gravity.NO_GRAVITY;
	private ArrayList<PublicNumMenu> arrayList = new ArrayList<PublicNumMenu>();
	private LayoutInflater layoutInflater = null;

	/**
	 * 
	 * @param context
	 * @param width
	 * @param height
	 * @param _oneTextViewStr
	 * @param _twoTextViewStr
	 * @param _oneImageViewResourcesId
	 * @param _twoImageViewResourcesId
	 * @param _IPopupWindowListener
	 */
	public MyPopupWindowBottom(Context context,
			ArrayList<PublicNumMenu> _arrayList,
			IPublicNumClickListener _IPopupWindowListener) {
		super(context);
		this.iPopupWindowListener = _IPopupWindowListener;
		this.mContext = context;
		arrayList = _arrayList;
		layoutInflater = LayoutInflater.from(context);
		// 设置可以获得焦点
		setFocusable(true);
		// 设置弹窗内可点击
		setTouchable(true);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		// 设置弹窗外可点击
		setOutsideTouchable(true);
		// 设置弹窗的宽度和高度
		setBackgroundDrawable(new BitmapDrawable());
		// 设置弹窗的布�?界面
		setContentView(LayoutInflater.from(mContext).inflate(
				R.layout.layout_pop_bottom, null));
		initUI();
	}

	/**
	 * 初始化弹窗列�?
	 */
	private void initUI() {
		PopAdapter mAdapter = new PopAdapter();
		ListView listview = (ListView) getContentView().findViewById(
				R.id.listview);
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				PublicNumMenu popupEntity = (PublicNumMenu) arg0.getAdapter()
						.getItem(arg2);
				if (popupEntity != null) {
					iPopupWindowListener.onPublicNumClick(popupEntity.url);
					dismiss();
				}
			}
		});
	}

	/**
	 * 显示弹窗列表界面
	 */
	public void show(View view) {
		initUI();
		// 获得点击屏幕的位置坐�?
		view.getLocationOnScreen(mLocation);
		// 设置矩形的大�?
		mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
				mLocation[1] + view.getHeight());
		// 显示弹窗的位�?
		showAtLocation(view, popupGravity,
				mLocation[0] ,
				mRect.bottom - QyxWeightDensityUtils.dp2px(mContext, 136.0f));
	}

	private class PopAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return arrayList == null ? 0 : arrayList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arrayList == null ? null : arrayList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			PublicNumMenu mPublicNumMenu = arrayList.get(arg0);
			PopViewHodler hodler = null;
			if (arg1 == null) {
				hodler = new PopViewHodler();
				arg1 = layoutInflater.inflate(R.layout.item_pop_bottom_layout,
						arg2,false);
				hodler.pop_tv = (TextView) arg1.findViewById(R.id.pop_tv);
				arg1.setTag(hodler);
			} else {
				hodler = (PopViewHodler) arg1.getTag();
			}
			hodler.pop_tv.setText(mPublicNumMenu.text);
			return arg1;
		}

		private class PopViewHodler {
			private TextView pop_tv;
		}
	}
	
	public interface IPublicNumClickListener{
		abstract void onPublicNumClick(String url);
	};
}